﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;

namespace HdipCricketClub.Controllers
{
    public class ResultsController : Controller
    {
        CricketClubEntities db = new CricketClubEntities();

        FixtureRepository fixtureRepository = new FixtureRepository();
        ClubFixturePlayerRepository clubFixturePlayerRepository = new ClubFixturePlayerRepository();
        ClubOtherFixtureDetailRepository clubOtherFixtureDetailRepository = new ClubOtherFixtureDetailRepository();
        OpponentFixturePlayerRepository opponentFixturePlayerRepository = new OpponentFixturePlayerRepository();
        OpponentOtherFixtureDetailRepository opponentOtherFixtureDetailRepository = new OpponentOtherFixtureDetailRepository();

        //
        // GET: /Results/

        public ActionResult Index()
        {
            return View();
        }

        //
        //  GET: /Results/ViewScoreCards/

        public ActionResult ViewScorecards()
        {
            var fixtures = fixtureRepository.FindFixtureScorecards();

            return View(fixtures.ToList());
        }

        //
        //  GET: /Results/GetScorecard/

        public ActionResult GetScorecard(int id)
        {
            CompletedScoreCardViewModel completedScorecardViewModel = new CompletedScoreCardViewModel();
            completedScorecardViewModel.CurrentFixture = fixtureRepository.GetFixture(id);
            completedScorecardViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsman(id);
            completedScorecardViewModel.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(id);
            completedScorecardViewModel.ClubOtherDetails = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetails(id);
            completedScorecardViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(id);
            completedScorecardViewModel.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(id);
            completedScorecardViewModel.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetails(id);

            double subTotalClubBatsmen = clubFixturePlayerRepository.TotalBatsmensRuns(id);
            ViewBag.SubTotalClubBatsmen = subTotalClubBatsmen;
            double totalClubExtras = clubOtherFixtureDetailRepository.ClubTotalExtras(id);
            ViewBag.TotalClubExtras = totalClubExtras;
            ViewBag.TotalClubRuns = subTotalClubBatsmen + totalClubExtras;

            double subTotalOpponentBatsmensRuns = opponentFixturePlayerRepository.TotalOpponentBatsmensRuns(id);
            ViewBag.SubTotalOpponentBatsmensRuns = subTotalOpponentBatsmensRuns;
            double totalOpponentExtras = opponentOtherFixtureDetailRepository.OpponentTotalExtras(id);
            ViewBag.TotalOpponentExtras = totalOpponentExtras;
            ViewBag.TotalOpponentRuns = subTotalOpponentBatsmensRuns + totalOpponentExtras;

            return View(completedScorecardViewModel);
        }

        //
        //  GET:    Results/ViewTables/

        public ActionResult ViewTables()
        {
            return View();
        }
    }
}
